package com.sunmi.paymentdemo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.qmuiteam.qmui.widget.textview.QMUILinkTextView;
import com.sunmi.paymentdemo.BuildConfig;
import com.sunmi.paymentdemo.R;
import com.sunmi.paymentdemo.bean.Config;
import com.sunmi.paymentdemo.bean.Request;
import com.sunmi.paymentdemo.view.CustomEditText;
import com.sunmi.paymentdemo.view.CustomRadioButton;
import com.sunmi.paymentdemo.view.CustomTextView;


/**
 * 发起L3消费交易
 */
public class ConsumeActivity extends BaseActivity {
    private CustomEditText consume_polling, consume_appType, consume_amount, consume_orderId, consume_businessId,
            consume_orderInfo, consume_payCode, consume_printIdType, consume_remarks;
    private CustomTextView consume_transType, consume_appId;
    private CustomRadioButton consume_neddPolling, consume_resultDisplay, consume_processDisplay, consume_printTicket, consume_voiceBroadcast;
    private QMUIRoundButton bt_consume_start, bt_consume_new_orderId;
    private QMUILinkTextView tv_consume_request, tv_consume_result, tv_cancel_result, consume_warning;
    private QMUITopBarLayout consume_topbar;

    private boolean isSunmi = true;
    /**
     * 内部测试使用
     */
    private QMUIRoundButton btn_dev, btn_test, btn_uat, btn_release;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_consume);
        initView();
        mLoadingDialog.setDialogListener(() -> {
            Log.e("yiuhet", "cancel pay");
            Request request = new Request();
            // 应用包名
            request.appId = getPackageName();
            // 交易类型
            request.transType = "10";
            request.oriOrderId = consume_orderId.getContent();
            Gson gson = new Gson();
            String jsonStr = gson.toJson(request);
            callPayment(jsonStr);
        });
        mLoadingDialog.setCancelBtnShow(true);
    }

    private void initView() {
        consume_topbar = findViewById(R.id.consume_topbar);
        consume_topbar.setTitle(R.string.home_string_consume);
        consume_topbar.addLeftBackImageButton().setOnClickListener(v -> finish());

        consume_appType = findViewById(R.id.consume_appType);
        consume_appId = findViewById(R.id.consume_appId);
        consume_transType = findViewById(R.id.consume_transType);
        consume_polling = findViewById(R.id.consume_polling);
        consume_amount = findViewById(R.id.consume_amount);
        consume_orderId = findViewById(R.id.consume_orderId);
        consume_businessId = findViewById(R.id.consume_businessId);
        consume_orderInfo = findViewById(R.id.consume_orderInfo);
        consume_payCode = findViewById(R.id.consume_payCode);
        tv_consume_request = findViewById(R.id.tv_consume_request);
        consume_neddPolling = findViewById(R.id.consume_needPolling);
        consume_processDisplay = findViewById(R.id.consume_processDisplay);
        consume_resultDisplay = findViewById(R.id.consume_resultDisplay);
        consume_warning = findViewById(R.id.consume_warning);
        consume_printTicket = findViewById(R.id.consume_printTicket);
        consume_voiceBroadcast = findViewById(R.id.consume_voiceBroadcast);
        consume_printIdType = findViewById(R.id.consume_printIdType);
        consume_remarks = findViewById(R.id.consume_remarks);
        consume_appType.setText(R.string.title_string_appType, R.string.indicator_must);
        consume_appId.setText(R.string.title_string_appId, "", R.string.indicator_must);
        consume_transType.setText(R.string.title_string_transType, "00", R.string.indicator_must);
        consume_amount.setText(R.string.title_string_amount, R.string.indicator_must);
        consume_orderId.setText(R.string.title_string_orderId, R.string.indicator_optional);
        consume_businessId.setText(R.string.title_string_businessId, R.string.indicator_optional);
        consume_orderInfo.setText(R.string.title_string_orderInfo, R.string.indicator_optional);
        consume_payCode.setText(R.string.title_string_payCode, R.string.indicator_optional);
        consume_polling.setText(R.string.title_string_edit_polling, R.string.indicator_optional);
        consume_polling.setContent("polling");
        consume_printTicket.setText(R.string.title_string_printTicket, R.string.indicator_optional);
        consume_printTicket.setTitle(R.string.title_string_print_ticket, R.string.title_string_unprint_ticket);

        consume_voiceBroadcast.setTitle(R.string.title_string_voice_broadcast_open, R.string.title_string_voice_broadcast_close);
        consume_voiceBroadcast.setText(R.string.title_string_voiceBroadcast, R.string.indicator_optional);

        consume_neddPolling.setTitle(R.string.title_string_polling, R.string.title_string_not_polling);
        consume_neddPolling.setText(R.string.title_string_needPolling, R.string.indicator_optional);

        consume_processDisplay.setTitle(R.string.title_string_process_display, R.string.title_string_process_undisplay);
        consume_processDisplay.setText(R.string.title_string_processDisplay, R.string.indicator_optional);

        consume_resultDisplay.setTitle(R.string.title_string_result_display, R.string.title_string_result_undisplay);
        consume_resultDisplay.setText(R.string.title_string_resultDisplay, R.string.indicator_optional);

        consume_printIdType.setText(R.string.title_string_printIdType, R.string.indicator_optional);

        consume_remarks.setText(R.string.title_string_remarks, R.string.indicator_optional);

        consume_warning.setText(R.string.string_warning);

        bt_consume_start = findViewById(R.id.bt_consume_start);
        bt_consume_start.setOnClickListener(this);
        bt_consume_new_orderId = findViewById(R.id.bt_consume_new_orderId);
        bt_consume_new_orderId.setOnClickListener(this);

        tv_consume_result = findViewById(R.id.tv_consume_result);
        tv_cancel_result = findViewById(R.id.tv_cancel_result);

        consume_neddPolling.setChecked(true);
        consume_neddPolling.setVisibility(View.GONE);
        consume_processDisplay.setChecked(true);
        consume_resultDisplay.setChecked(true);
        consume_printTicket.setChecked(true);
        consume_voiceBroadcast.setChecked(true);

        //内部测试
        if (BuildConfig.BUILD_TYPE.equals("envTest")) {
            findViewById(R.id.test_ll_type).setVisibility(View.VISIBLE);
            btn_dev = findViewById(R.id.btn_type_dev);
            btn_test = findViewById(R.id.btn_type_test);
            btn_uat = findViewById(R.id.btn_type_uat);
            btn_release = findViewById(R.id.btn_type_release);
            btn_dev.setOnClickListener(this);
            btn_test.setOnClickListener(this);
            btn_uat.setOnClickListener(this);
            btn_release.setOnClickListener(this);
            bt_consume_new_orderId.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // 自动生一个随机的Saas订单号，并填入
            case R.id.bt_consume_new_orderId:
                createNewOrderId("");
                break;
            /**
             * 开始消费交易
             */
            case R.id.bt_consume_start:
                tv_consume_result.setText("");
                Gson gson = new Gson();
                String jsonStr = gson.toJson(convertRequest());
                callPayment(jsonStr);
                tv_consume_request.setText(getString(R.string.message_string_request) + jsonStr);
                break;
            case R.id.btn_type_dev:
                createNewOrderId("dev_");
                break;
            case R.id.btn_type_test:
                createNewOrderId("test_");
                break;
            case R.id.btn_type_uat:
                createNewOrderId("uat_");
                break;
            case R.id.btn_type_release:
                createNewOrderId("");
                break;
        }
    }

    /**
     * 创建新的saas订单号
     *
     * @param preString 前缀
     */
    private void createNewOrderId(String preString) {
        String new_order_id = System.currentTimeMillis() / 1000 + "" + (int) (Math.random() * 9000 + 1000);
        consume_orderId.setContent(preString + new_order_id);
    }

    /**
     * 根据当前输入框生成请求参数
     *
     * @return
     */
    @Override
    protected Request convertRequest() {
        /**
         * 请求数据，最终转为json字符串发生到收银台
         */
        Request request = new Request();
        // 应用类型
        request.appType = consume_appType.getContent();
        // 应用包名
        request.appId = getPackageName();
        // 交易类型
        request.transType = consume_transType.getContent();
        // 交易金额
        Long amount = 0L;
        try {
            amount = Long.parseLong(consume_amount.getContent());
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.amount = amount;
        // Saas软件订单号
        request.orderId = consume_orderId.getContent();
        //Saas系统业务订单号
        request.businessId = consume_businessId.getContent();
        // 商品信息
        request.orderInfo = consume_orderInfo.getContent();
        // 支付码
        request.payCode = consume_payCode.getContent();
        Config config = new Config();
        config.pollingType = consume_polling.getContent();
        // 交易过程中是否显示UI界面
        config.processDisplay = consume_processDisplay.getContent();
        // 是否展示交易结果页
        config.resultDisplay = consume_resultDisplay.getContent();
        // 是否打印小票
        config.printTicket = consume_printTicket.getContent();
        //是否打开语音播报开关
        config.voiceBroadcast = consume_voiceBroadcast.getContent();
        // 指定签购单上的退款订单号类型
        config.printIdType = consume_printIdType.getContent();
        // 备注
        config.remarks = consume_remarks.getContent();
        request.config = config;
        return request;
    }

    @Override
    protected void onReceive(String result) {
        tv_consume_result.setText(tv_consume_result.getText() + "\n\n 结果: \n" + result);
    }
}
